<template>
  <div>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="state.queryForm.tableName" placeholder="表名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gencode( )">直接生成代码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="Waming" @click="getSalaryDataAgg( )">获取主子表</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="Waming" @click="updSalaryData( )">更新信息</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="delSalaryData( )">del信息</el-button>
      </el-form-item>

    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%"
              @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>

      <el-table-column prop="tablename" label="表名" header-align="center" align="center" width="300">
        <template #default="scope">
          <el-link prop="engine" @click="fieldInfo(scope.row.tablename)" target='_blank' underline='false'>
            {{ scope.row.tablename }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="tablecomments" label="表说明" header-align="center" align="center"/>
      <el-table-column prop="classname" label="类名" header-align="center" align="center"/>
      <el-table-column prop="createtime" label="创建时间" header-align="center" align="center"/>
      <!--<el-table-column prop="tabletype" label="类型(表/视图)" header-align="center" align="center" width="150"/>-->
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template #default="scope">
          <el-button type="primary" size="small" @click="generator(scope.row.tablename) ">生成代码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="state.page"
        :page-sizes="state.pageSizes"
        :page-size="state.limit"
        :total="state.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>

    <generator ref="generatorRef" @refreshDataList="getDataList"></generator>
  </div>
</template>

<script setup lang="ts" name="DataSource">
import {reactive, ref} from 'vue'
import {IHooksOptions} from '@/hooks/interface'
import {useCrud} from '@/hooks'
import {ElMessage} from "element-plus/es";
import {
  usedelSalaryDataAPI,
  useGeneratorAPI,
  usegetSalaryDataAggAPI,
  useGrnCodeAPI,
  useTableFieldApi,
  useupdSalaryDataAggAPI
} from "@/api/generator";

const state: IHooksOptions = reactive({
  dataListUrl: '/gen/table/page',
  queryForm: {
    tableName: ''
  }
})

const generatorRef = ref()
const generatorHandle = (id?: number) => {
  generatorRef.value.init(id)
}

const {getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle} = useCrud(state)

function fieldInfo(tableName?: any) {
  console.log(tableName);
  useTableFieldApi(tableName).then((result: any) => {
    console.log("fieldInfo查看字段详情...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}

const generator = function (tableName ?: any) {
  console.log(tableName);
  useGeneratorAPI(tableName).then((result: any) => {
    console.log("generator生成代码(行)...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}
const gencode = function (tableName ?: any) {
  useGrnCodeAPI(tableName).then((result: any) => {
    console.log("gencode直接生成代码...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}
const getSalaryDataAgg = function (tableName ?: any) {
  usegetSalaryDataAggAPI(tableName).then((result: any) => {
    console.log("getSalaryDataAgg...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}

const updSalaryData = function (tableName ?: any) {
  useupdSalaryDataAggAPI(tableName).then((result: any) => {
    console.log("updSalaryData...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}
const delSalaryData = function (tableName ?: any) {
  usedelSalaryDataAPI(tableName).then((result: any) => {
    console.log("delSalaryData...");
    console.log(result);
    ElMessage.success(result.msg)
  });
}

</script>
